package vue;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.ArrayList;


import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;


public class VueStatistique extends FourmizFrame{

	private JPanel panelNbfourmies,panelNbFourmiesMoy,panelQtNourriture;
	private JScrollPane jscNbfourmies,jscNbFourmiesMoy,jscQtNourriture;
	private ArrayList<Integer> list_nb_fourmis, list_nb_fourmis_moyen, list_quantite_nourriture;
	
	public VueStatistique(ArrayList<Integer> list_nb_four, ArrayList<Integer> list_nb_four_moyen, ArrayList<Integer> list_quantite_nou) {
		super("Statistiques de la simulation");
		setSize(1200,900);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setLocationRelativeTo(null);
		list_nb_fourmis=list_nb_four;
		list_nb_fourmis_moyen=list_nb_four_moyen;
		list_quantite_nourriture=list_quantite_nou;
		initVue();
		setLayout(new GridLayout(3, 1));
		setVisible(true);
		
		
	}
	
	public void initVue(){
		panelNbfourmies=new JPanel(new BorderLayout());
		panelNbFourmiesMoy=new JPanel(new BorderLayout());
		panelQtNourriture=new JPanel(new BorderLayout());
		
		jscNbfourmies=new JScrollPane(panelNbfourmies);
		jscNbFourmiesMoy=new JScrollPane(panelNbFourmiesMoy);
		jscQtNourriture=new JScrollPane(panelQtNourriture);
		
		panelNbfourmies.setBorder(BorderFactory.createLineBorder(Color.black));
		panelNbFourmiesMoy.setBorder(BorderFactory.createLineBorder(Color.black));
		panelQtNourriture.setBorder(BorderFactory.createLineBorder(Color.black));
		
		panelNbfourmies.add(new JLabel("Nombre de fourmis :"),BorderLayout.PAGE_START);
		panelNbFourmiesMoy.add(new JLabel("Nombre de fourmis moyen :"),BorderLayout.PAGE_START);
		panelQtNourriture.add(new JLabel("Quantité de nourriture :"),BorderLayout.PAGE_START);
		
		XYDataset dataset = createDataset();
        JFreeChart chart = createChart(dataset);
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new Dimension(1100, 270));
        panelNbfourmies.add(chartPanel,BorderLayout.CENTER);
        
        XYDataset dataset2 = createDataset2();
        JFreeChart chart2 = createChart(dataset2);
        final ChartPanel chartPanel2 = new ChartPanel(chart2);
        chartPanel2.setPreferredSize(new Dimension(1100, 270));
        panelNbFourmiesMoy.add(chartPanel2,BorderLayout.CENTER);
        
        XYDataset dataset3 = createDataset3();
        JFreeChart chart3 = createChart(dataset3);
        final ChartPanel chartPanel3 = new ChartPanel(chart3);
        chartPanel3.setPreferredSize(new Dimension(1100, 270));
        panelQtNourriture.add(chartPanel3,BorderLayout.CENTER);
		
		
		add(jscNbfourmies);
		add(jscNbFourmiesMoy);
		add(jscQtNourriture);
	}
    
    /**
     * Creates a sample dataset.
     * 
     * @return a sample dataset.
     */
    private XYDataset createDataset() {
        
        final XYSeries series1 = new XYSeries("Nombre de fourmis ");
        int i=0;
        for(Integer nb:list_nb_fourmis){
        	series1.add(i,(double)nb);
        	i++;        	
        }

        final XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(series1);
                
        return dataset;
        
    }
    
private XYDataset createDataset2() {
        
        

        final XYSeries series2 = new XYSeries("Nombre de fourmis moyen");
        int i=0;
        for(Integer nb:list_nb_fourmis_moyen){
        	series2.add(i,(double)nb);
        	i++;        	
        }

        final XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(series2);
                
        return dataset;
        
    }

private XYDataset createDataset3() {
    
    
    final XYSeries series3 = new XYSeries("Quantité de nourriture");
    int i=0;
    for(Integer nb:list_quantite_nourriture){
    	series3.add(i,(double)nb);
    	i++;        	
    }

    final XYSeriesCollection dataset = new XYSeriesCollection();
    dataset.addSeries(series3);
            
    return dataset;
    
}
    
    /**
     * Creates a chart.
     * 
     * @param dataset  the data for the chart.
     * 
     * @return a chart.
     */
    private JFreeChart createChart(final XYDataset dataset) {
        
        // create the chart...
        final JFreeChart chart = ChartFactory.createXYLineChart(
            "",      // chart title
            "TICK",                      // x axis label
            "Valeur",                      // y axis label
            dataset,                  // data
            PlotOrientation.VERTICAL,
            true,                     // include legend
            true,                     // tooltips
            false                     // urls
        );

        // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
        chart.setBackgroundPaint(Color.white);

//        final StandardLegend legend = (StandardLegend) chart.getLegend();
  //      legend.setDisplaySeriesShapes(true);
        
        // get a reference to the plot for further customisation...
        final XYPlot plot = chart.getXYPlot();
        plot.setBackgroundPaint(Color.lightGray);
    //    plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);
        
//        final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
//        renderer.setSeriesLinesVisible(0, false);
//        renderer.setSeriesShapesVisible(1, false);
//        plot.setRenderer(renderer);

        // change the auto tick unit selection to integer units only...
        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        // OPTIONAL CUSTOMISATION COMPLETED.
                
        return chart;
        
    }
	
//	public static void main(String[] args) {
//		new VueStatistique();
//	}
	
}
